import Scene from "./Scene.js"
import config from "../config.js";

//游戏整体的控制
export default class Game{ //游戏的类
    constructor(canvas) { //用一个形参canvas 接收 传入的实参canvas
        this.canvas = canvas
        this.canvas.width = config.width
        this.canvas.height = config.height
        this.ctx = this.canvas.getContext("2d")
    }
    restart(){ //游戏的初始化

        //游戏场景初始化
        this.scene = new Scene(this.ctx) //新建场景，从构造函数Scene中实例化出一个新的场景对象，并将canvas传进去

        //执行render，开始渲染
        this.render()
    }
    render(){ //游戏的渲染

        //关键帧回调
        requestAnimationFrame(() => this.render())//用箭头函数强行绑定上下文

        //每次渲染新的背景图片之前，先清空上次渲染的背景图片，即设表先关
        this.ctx.clearRect(0,0,config.width,config.height)
        this.scene.render()
    }
}